package com.javapda.app.camera.gui;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Properties;


/**
 * This is the launch point for the camera application
 * User decides whether to use an interactive mode or a gui mode based
 * on the command-line parameters provided.
 * 
 * Configuration may be done in the following ways:
 * <ul>
 *   <li>Property file [Default= ${HOME}/.camera/camera.properties]</li>
 *   <li>Command-line</li>
 *   <li>From gui</li>
 * </ul>
 * 
 * @author javapda
 *
 */
public class CameraApp {
	private static final String CAMERA_APP_PROPERTIES = "camera.app.properties";
	private static final String CR = "\n";
	public static void main(String[] args) {
		help();
		// load properties
		loadProperties();
		if ( args.length == 0 ) {
			processArgs(args);
		}
		//new CameraFrame().setVisible(true);
	}
	private static void loadProperties() {
//		System.getProperties().list(System.out);
		File cameraHomeDir = new File(System.getProperty("user.home"),".camera");
		if ( !cameraHomeDir.exists()) {
			boolean res = cameraHomeDir.mkdir();
			if ( !res ) {
				String msg = "Unable to create directory: " + cameraHomeDir.getAbsolutePath();
				System.out.println(msg);
				throw new RuntimeException(msg);
			}
		}
		File propFile = new File(cameraHomeDir,CAMERA_APP_PROPERTIES);
		if ( propFile.exists() ) {
			// load it
		} else {
			// create it
			if ( cameraHomeDir.canWrite() ) {
				// copy contents of camera.app.properties to the new file
				InputStream is = CameraApp.class.getClassLoader().getResourceAsStream("com/javapda/app/camera/gui/"+CAMERA_APP_PROPERTIES);
				Properties props = new Properties();
				try {
					props.load(is);
				} catch (IOException e) {
					e.printStackTrace();
				}
				props.list(System.out);
				BufferedWriter bw = null;
				FileWriter fw = null;
				PrintWriter pw = null;
				try {
					fw=new FileWriter(propFile);
					pw = new PrintWriter(fw);
					pw.print("#");
					props.list(pw);
					pw.flush();
					pw.close();
				} catch (IOException e) {
					e.printStackTrace();
				} finally {
					// cleanup?
				}
				
			}
		}
	}
	private static void processArgs(String[] args) {
		// if gui desired then 
	}
	private static void help() {
		Map map = new LinkedHashMap();
		map.put("--propertyfile", "use the specified property file");
		map.put("--show-errornumbers", "present known error numbers");
		map.put("--cmd-poweroff", "places camera in sleep mode");
		map.put("--cmd-poweron", "awakens camera (SYNC operation)");
		map.put("--help", "show this help");
		map.put("--gui", "use graphical user interface");
		map.put("--cfg-compressed", "configure compression");
		map.put("--cfg-uncompressed", "configure no compression");
		map.put("--cfg-resolution", "configure resolution (80x64,160x128,320x240,640x480)");
		map.put("--cfg-picturetype", "configure picture type (snapshot,preview,jpeg_preview");
		map.put("--cfg-lightfrequency", "configure light frequency (50HZ,60HZ)");
		map.put("--op-snapshot-interval","delay (seconds) between snapshots");
		map.put("--op-snapshot-count","number of snapshots to take (if 0, then indefinite)");
		
		StringBuilder sb = new StringBuilder();
		sb.append("Usage:").append(CR);
		sb.append("  CameraApp [OPTION]").append(CR);
		sb.append("Options:").append(CR);
		System.out.println(sb);
		for (Iterator iterator = map.keySet().iterator(); iterator.hasNext();) {
			String key = (String) iterator.next();
			System.out.println("  "+key+"      "+map.get(key));
		}
	}
}
